<?php

function create_data_tarif_pajak($id =0){
	 
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	$view = form_header( "tarif_pajak" , "tarif_pajak"  );
 	$fields = my_get_data_by_id('pr_tarif_pajak','tarif_pajak_id',$id);
	$label=' ';	
	if(isset( $fields['label_range'] )){
		$tarifs = my_fetch_array(my_query(
			"SELECT label_range
			from pr_tarif_pajak 
			WHERE label_range> {$fields['label_range']} 
			ORDER BY label_range LIMIT 1"
			)) ;
		if( $tarifs['label_range']>0){
			$label=' - '. rp_format($tarifs['label_range']) ;
		}			
	}

	$label_range = array(
			'name'=>'label_range',
			'value'=>(isset($_POST['label_range'])? $_POST['label_range'] : rp_format($fields['label_range'])),
			'id'=>'label_range',
			'type'=>'textfield',
			'size'=>'15'
		);
	$form_label_range = form_money($label_range);
	$view .= form_field_display( $form_label_range."&nbsp;".$label  , "Rupiah"  );
	
	$tarif = array(
			'name'=>'tarif',
			'value'=>(isset($_POST['tarif'])? $_POST['tarif'] : $fields['tarif']),
			'id'=>'tarif',
			'type'=>'textfield',
			'size'=>'5'
		);
	$form_tarif = form_dynamic($tarif); 
	 
	 
	$view .= form_field_display( $form_tarif."&nbsp;%"  , "Tarif"  );
	 
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function form_tarif_pajak_validate($id){
	$errsubmit = false;
	$err = array();
	 
	$code =  trim($_POST["label_range"]); 
	if( $code == ''){
		$errsubmit = true;
		$err[] = "Rupiah belum di isi";
	}
	$query="SELECT tarif_pajak_id FROM pr_tarif_pajak 
		WHERE label_range='".$code.
		"' AND tarif_pajak_id<>".$id; 
	if (my_num_rows( my_query($query)) >0){
		$errsubmit = true;
		$err[] = "Rupiah sudah digunakan";	
	}
	 
	$tarif = trim($_POST["tarif"]);
	if($tarif == ''){
		$errsubmit = true;
		$err[] = "Tarif belum diisi";
	}
	else
	if( (int) $tarif > 100){
		$errsubmit = true;
		$err[] = "Nilai tarif terlalu besar"; 
	}elseif(!is_numeric($tarif)){
		$errsubmit = true;
		$err[] = "Tarif bukan angka"; 
	}

	if( $errsubmit){
		return $err;
	}
	return $errsubmit;
}

function create_tarif_pajak_submit($id = 0){

	$datas = array(
		'label_range'=>my_type_data_str( set_text_as_number($_POST['label_range'])),
		'tarif'=>my_type_data_str($_POST['tarif']),
	);
	
	if( $id > 0 ){
		return my_update_record('pr_tarif_pajak' ,  'tarif_pajak_id', $id , $datas);
	}
	return my_insert_record( 'pr_tarif_pajak' , $datas);
}

function daftar_tarif_pajak(){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'text-align:center;border-bottom:2px solid;width:5%'),  
		'Rupiah'=>array('style'=>'text-align:left;border-bottom:2px solid;width:25%'),   
		'Tarif (%)'=>array('style'=>'text-align:right;border-bottom:2px solid;width:55%'),  
		' '=>array('style'=>'text-align:center;border-bottom:2px solid;width:15%'), 
	);

	$query = "SELECT * FROM pr_tarif_pajak  ORDER BY label_range
	 ";
	$result = my_query($query);
	$i = 0;
	$row=array();
	while($ey = my_fetch_array($result)){
		$i++;
		$editproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=edit&id='.$ey['tarif_pajak_id'],
			'rel'=>'facebox',
			'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		 

		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['tarif_pajak_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		$label_label_range=get_label_label_range($ey['label_range'])		;
		$row[] = array(
			'#'=>position_text_align ($i, 'center'),  
			//'label_range'=>  position_text_align( $label_label_range ,   'center' ),
			'label_range'=> $label_label_range ,   
			'tarif'=>  position_text_align( $ey['tarif']."%"  ,   'right' ),			
			'operasi'=> position_text_align(  $edit_button .' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	return table_builder($header , $datas ,  4, false );
}


function delete_tarif_pajak($tarif_pajak_id){
	$query = "DELETE FROM pr_tarif_pajak WHERE tarif_pajak_id ={$tarif_pajak_id} ";
	return my_query($query);
}	

function get_label_label_range($label_range){

	$tarifs = my_fetch_array(my_query(
		"SELECT label_range
		from pr_tarif_pajak 
		WHERE label_range> {$label_range} 
		ORDER BY label_range LIMIT 1"
		)) ;
	if( $tarifs['label_range']>0){
		return '> '.rp_format($label_range).' - <='. rp_format($tarifs['label_range']) ;
	}
	return ' Diatas '.rp_format($label_range) ;
}
